﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace Draco.Commons.Reflection
{
    /// <summary>
    /// Class封装
    /// </summary>
    public class Class
    {
        /***********************************************************************
         * 静态方法
         ***********************************************************************/
        #region 
        /// <summary>
        /// 获取Class类型
        /// </summary>
        /// <param name="clazz"></param>
        /// <returns></returns>
        public static Class ForName(String clazz)
        {
            AssemblyName assemblyName = ReflectUtils.ParseAssemblyName(clazz);
            String ClazzName = ReflectUtils.ParseClassName(clazz);
            Assembly assembly = ReflectFactory.GetAssembly(assemblyName);
            Type type = ReflectFactory.GetType(assembly, ClazzName);
            return type == null ? null : new Class(type);
        }
        /// <summary>
        /// 获取方法
        /// </summary>
        /// <param name="DelegateString"></param>
        /// <returns></returns>
        public static MethodInfo GetMethod(string DelegateString)
        {
            AssemblyName dll = ReflectUtils.ParseAssemblyName(DelegateString);
            string typeName = ReflectUtils.ParseClassName(DelegateString);
            string methodName = ReflectUtils.ParseMethodName(DelegateString);
            if (dll == null || String.IsNullOrEmpty(typeName) || String.IsNullOrEmpty(methodName))
            {
                return null;
            }
            MethodInfo m = ReflectFactory.GetMethod(dll, typeName, methodName);
            return m;
        }
        #endregion

        /***********************************************************************
         * 实例方法
         ***********************************************************************/
        private Type m_type;

        /// <summary>
        /// 获取类型
        /// </summary>
        public Type Type
        {
            get { return m_type; }
        }

        /// <summary>
        /// 构造
        /// </summary>
        /// <param name="clazz"></param>
        public Class(Type clazz)
        {
            m_type = clazz;
        }
        /// <summary>
        /// 创建新对象
        /// </summary>
        /// <returns></returns>
        public object NewInstance()
        {
            return NewInstance(null);
        }
        /// <summary>
        /// 创建新对象
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public object NewInstance(params object[] args )
        {
            return ReflectFactory.GetObject(m_type, args);
        }
        /// <summary>
        /// 判断是否是从给定类型派生的
        /// </summary>
        /// <param name="type">基类类型</param>
        /// <returns></returns>
        public bool IsSubClassOf(Type type)
        {
            if (type.IsInterface)
            {
                Type target = m_type.GetInterface(type.FullName);
                return target != null;
            }
            else
            {
                return m_type.IsSubclassOf(type);
            }
        }

        
    }
}
